#!/usr/bin/node

const log = console.log,
      http = require('https'),
      city = process.argv[2] ||'石家庄',
      addr = 'https://api.66mz8.com/api/weather.php?location=' + city,
      //addr = 'https://api.66mz8.com/api/weather.php?location=%e7%9f%b3%e5%ae%b6%e5%ba%84',
      url = require('url');

log(addr);

let addr1 = global.encodeURI(addr);
log(addr1);

let opt = url.parse(addr1);
log('opt:',opt);
opt.headers={
  'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
};
log('opt.headers:',opt.headers);

http.get(opt,res=>{
  log(`HTTP/${res.httpVersion} ${res.statusCode} ${res.statusMessage}`);
  log(res.headers);

  let result = '';

  res.on('data', (data) => {
    result += data.toString('utf8');
          
  });

  res.on('end', () => {
    let weather = JSON.parse(result);
    console.log(weather);       
  });
})
